<?php
namespace app\admin\controller;


class Article extends Base
{
    public function catelists(){
        $article=model("article");
        $totalcount=$article->count();
        $list=$upcatelist=model("cate")->where(['cms_id'=> $this->cms['id']])->order("sort asc,id asc")->select();
        $countarr=$article->field('count(*) as count,cate_id')->group("cate_id")->select();
        $countlist=[];
        foreach ($countarr as $_c){
            $countlist[$_c['cate_id']]=$_c['count'];
        }
        for($i=0;$i<count($list);$i++){
            $subcount=model("cate")->where("upid",$list[$i]['id'])->count();
            if($subcount==0){
                $list[$i]['hassub']=0;
            }
            else{
                $list[$i]['hassub']=1;
            }
            if(!empty($countlist[$list[$i]['id']])){
                $list[$i]['count']=$countlist[$list[$i]['id']];
                $totalcount-=(int)$list[$i]['count'];
            }else{
                $list[$i]['count']=0;
            }

        }
      
        $this->assign("list",$list);
        return view();
    }
    public function  lists(){
        $this->assign("cate_id",input("id"));
        $this->_list("article",['cms_id'=> $this->cms['id'],'cate_id'=> input("id")],"sort desc,id desc");
        return view();
    }
    public function edit(){
        $data=['cms_id'=>$this->cms['id'],'cate_id'=>input("get.cate_id")];
        $thumb=$this->qiniuupload();
        if($thumb!=null){
            $data['thumb']=$thumb;
        }
        if($this->_edit("article",$data)){
            $this->redirect("article/lists","id=".input("get.cate_id"));
        };
        return view();
    }
}
